import {ref, computed} from 'vue'
import {defineStore} from 'pinia'
import { useCartStore } from '@/stores'

export const useUserStore = defineStore('user', () => {
  const token = ref('')
  const mem = ref({
    id: '',
    name: '',
    avatar: '',
    mobile: '',
    level: '',
    regcode: '',
    member_level: ''
  })
  // 是否显示登录弹窗
  const isLogin = ref(false)
  const loginToggle = (bool) => {
    if (bool) document.body.style.overflow = 'hidden'
    else document.body.style.overflow = 'auto'
    isLogin.value = bool
  }
  const setMember = (m) => {
    token.value = m?.token?.access_token
    mem.value = m?.member
  }
  const logout = () => {
    const { clearCart } = useCartStore()
    token.value = ''
    isLogin.value = false
    mem.value = {
      id: '',
      name: '',
      avatar: '',
      mobile: '',
      level: '',
      regcode: '',
      member_level: ''
    }
    clearCart()
  }
  return {
    mem,
    token,
    isLogin,
    setMember,
    loginToggle,
    logout
  }
}, {
  persist: true
})